<?php

namespace app\api\controller\v1;

use app\jobs\OutPushJob;
use app\model\basic\Smscode;
use app\model\staff\Staff;
use app\Request;
use app\services\face\SmsService;
use crmeb\exceptions\ApiException;
use crmeb\utils\JwtAuth;
use think\facade\Validate;

class LoginController
{

<<<<<<< HEAD
  

    public function ver(Request  $request)
=======
    public function push()
    {
        // OutPushJob::
    }


    public function ver(Request $request)
>>>>>>> e6a5eba6ef03b01534c3684454ac9a77d45a4e93
    {
        $nowVer = 102;
        $ver = $request->param('version', '');
        if ($ver != $nowVer) {
            return app('json')->success([
                'version' => 'shengji',
                'linkurl' => $request->domain() . '/app/app102.apk',
            ]);
        }

        return app('json')->success([
            'version' => '',
            'linkurl' => '',
        ]);

    }

    public function login(Request $request)
    {
        [$company, $account, $password] = $request->postMore([
            'company',
            'number',
            'password',
            'spread'
        ], true);

        if (!$company) {
            return app('json')->fail('请输入企业id');
        }

        if (!$account || !$password) {
            return app('json')->fail('请输入账号密码');
        }
        if (strlen(trim($password)) < 6 || strlen(trim($password)) > 32) {
            return app('json')->fail('请输入6位以上密码');
        }
        $user = Staff::where('idcard', $account)->where('company_id', $company)->find();
        if ($user) {
            if ($user->password !== md5((string) $password)) {
                return app('json')->fail('密码不正确');
            }
            if ($user->status != 1) {
                return app('json')->fail('当前状态为禁用，不可登录');
            }
            if ($user->job_status != 1) {
                return app('json')->fail('当前不是在职状态，不可登录');
            }
        } else {
            return app('json')->fail('用户不存在');
        }
        $jwtAuth = app()->make(JwtAuth::class);
        $token = $jwtAuth->createToken($user['id'], 'api');

        $user->token = $token['token'];
        $user->login_ip = $request->ip();
        $user->login_time = date('Y-m-d H:i:s');
        $user->save();

        return app('json')->success([
            'token' => $token['token'],
        ]);
    }
    public function send(Request $request)
    {
        [$mobile] = $request->postMore([
            'mobile'
        ], true);
        if ($mobile == '' || Validate::is($mobile, 'mobile') == false) {
            return app('json')->fail('请输入正确手机号');
        }
        $code = rand(100000, 999999);

        $codeModel = new Smscode();
        $codeModel->save([
            'mobile' => $mobile,
            'code' => $code,
        ]);

        SmsService::sms($mobile, $code);

        return app('json')->success([
            'code' => '',
        ]);


    }
    public function pass(Request $request)
    {
        [$mobile, $idcard, $code, $pass, $pass2] = $request->postMore([
            ['mobile', ''],
            ['idcard', ''],
            ['code', ''],
            ['pass', ''],
            ['pass2', ''],
        ], true);

        $staff = Staff::where('mobile', $mobile)->where('idcard', $idcard)->find();
        if ($staff == []) {
            return app('json')->fail('身份证号跟手机号不一致');
        }
        if ($pass == '') {
            return app('json')->fail('请输入密码');
        }
        if ($pass2 == '') {
            return app('json')->fail('请输入确认密码');
        }
        if ($pass != $pass2) {
            return app('json')->fail('两次密码不正确');
        }

        if (Smscode::checkCode($mobile, $code) === false) {
            return app('json')->fail('验证码不正确');
        }

        $staff->password = md5($pass);
        $staff->save();
        return app('json')->success('找回成功');

    }
}